package com.qf.ayc.customers.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.ayc.customers.entity.Customers;
import com.qf.ayc.customers.mapper.CustomersMapper;
import com.qf.ayc.customers.service.ICustomersService;
import com.qf.ayc.user.vo.LoginVo;
import com.qf.ayc.utils.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author BeautyFairy
 * @since 2024-02-20
 */
@RestController
@CrossOrigin
@RequestMapping("/AYC/customers")
public class CustomersController {
    @Autowired
    private ICustomersService customersService;
    @Autowired
    private CustomersMapper customersMapper;
    @PostMapping("/add")
    public ResultVo add(Customers customers){

        customersService.add(customers);
        return ResultVo.ok("200","添加顾客成功");
    }
    @GetMapping("/login")
    public LoginVo login(String username, String password) {

        if (username == null || username.equals("")) {//如果传来的用户名是null，或者空字符串，throw一个错误
            throw new RuntimeException("用户名不合法");
        }
        if (password == null || password.equals("")) {//如果传来的密码是null，或者空字符串，throw一个错误
            throw new RuntimeException("密码不能为空");
        }

       LoginVo LoginVo =customersService.login(username, password);

        return  LoginVo;
    }

    @GetMapping("/register")
    public LoginVo register(String username, String password) {
        if (username.equals("") || password.equals("")) {
            return new LoginVo(-1, "必传参数为空", null);
        }
        boolean flag = customersService.regist(username, password);
        if (flag) {
            return new LoginVo(1, "ok", null);
        } else {
            return new LoginVo(-1, "用户名重复", null);
        }
    }



    @PostMapping("delete")
    public ResultVo delete(String id){
        customersMapper.deleteById(id);
        return ResultVo.ok("200","删除顾客成功");
    }

    @PostMapping("/update")
    public  ResultVo update(@RequestBody Customers customers){
        customersMapper.updateById(customers);
        return ResultVo.ok("200","修改顾客处成功",customers);
    }

    @PostMapping("/select")
    public ResultVo select(@RequestBody Customers customers){

        QueryWrapper<Customers> wrapper = new QueryWrapper<>();
        wrapper.eq(customers.getUsername()!=null,"username",customers.getUsername())
                .eq(customers.getNickname()!=null,"nickname",customers.getNickname());

        List<Customers> data = customersService.list(wrapper);

        return ResultVo.ok("200","搜索顾客成功",data);

    }

}
